<template>
	<view>
		<view
			id="_drag_button"
			class="drag"
			:style="'right: 0px; top:' + top + 'px;'"
			@touchstart="touchstart"
			@touchmove.stop.prevent="touchmove"
			@touchend="touchend"
			@click.stop.prevent="click"
			:class="{transition: isDock && !isMove }"
		>
      <view class="drag-bg">
        <image class="img-icon" src="./img/cus.png"></image>
        <view class="img-text">
          <view>订</view>
          <view>单</view>
          <view>投</view>
          <view>诉</view>
        </view>
      </view>
<!--      <view class="img-text">
        <view>联系小助手</view>
        <view>领报告指南</view>
      </view> -->
			<!-- <text>{{ text }}</text> -->
		</view>
    <customer-modal ref="cus"></customer-modal>
	</view>
</template>

<script>
  import customerModal from "@/components/customer-modal/customer-modal.vue";
	export default {
		name: 'drag-button',
		props: {
			isDock:{
				type: Boolean,
				default: false
			},
			existTabBar:{
				type: Boolean,
				default: false
			}
		},
		data() {
			return {
				top:0,
				left:0,
				width: 0,
				height: 0,
				offsetWidth: 0,
				offsetHeight: 0,
				windowWidth: 0,
				windowHeight: 0,
				isMove: true,
				edge: 150,
				text: '按钮'
			}
		},
		mounted() {
			const sys = uni.getSystemInfoSync();
			
			this.windowWidth = sys.windowWidth;
			this.windowHeight = sys.windowHeight;
			
			// #ifdef APP-PLUS
				this.existTabBar && (this.windowHeight -= 50);
			// #endif
			if (sys.windowTop) {
				this.windowHeight += sys.windowTop;
			}
			console.log(sys)
			
			const query = uni.createSelectorQuery().in(this);
			query.select('#_drag_button').boundingClientRect(data => {
				this.width = data.width;
				this.height = data.height;
				this.offsetWidth = data.width / 2;
				this.offsetHeight = data.height / 2;
				this.left = this.windowWidth - this.width - this.edge;
				this.top = this.windowHeight - this.height - this.edge;
			}).exec();
		},
		methods: {
			click() {
        this.$refs.cus.open()
				// this.$emit('btnClick');
			},
			touchstart(e) {
				// this.$emit('btnTouchstart');
			},
			touchmove(e) {
				// 单指触摸
				if (e.touches.length !== 1) {
					return false;
				}
				
				this.isMove = true;
				
				this.left = e.touches[0].clientX - this.offsetWidth;
				
				let clientY = e.touches[0].clientY - this.offsetHeight;
				// #ifdef H5
					clientY += this.height;
				// #endif
				let edgeBottom = this.windowHeight - this.height - this.edge;

				// 上下触及边界
				if (clientY < this.edge) {
					this.top = this.edge;
				} else if (clientY > edgeBottom) {
					this.top = edgeBottom;
				} else {
					this.top = clientY
				}
				
			},
			touchend(e) {
				if (this.isDock) {
					let edgeRigth = this.windowWidth - this.width - this.edge;
					
					if (this.left < this.windowWidth / 2 - this.offsetWidth) {
						this.left = this.edge;
					} else {
						this.left = edgeRigth;
					}
					
				}
				
				this.isMove = false;
				
				// this.$emit('btnTouchend');
			},
		}}
</script>

<style lang="scss">
	.drag {
		display: flex;
    flex-direction: column;
		justify-content: center;
		align-items: center;
		// background-color: rgba(0, 0, 0, 0.5);
		// box-shadow: 0 0 6upx rgba(0, 0, 0, 0.4);
		// color: $uni-text-color-inverse;
		width: 50rpx;
		height: 210rpx;
		// border-radius: 50%;
		// font-size: $uni-font-size-sm;
		position: fixed;
		z-index: 9999;
		
		&.transition {
			transition: left .3s ease,top .3s ease;
		}
	}
  .drag-bg {
    background-color: #313133;
    height: 100%;
    width: 100%;
    border-top-left-radius: 20rpx;
    border-bottom-left-radius: 20rpx;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    align-items: center;
  }
  .img-icon {
    height: 50rpx;
    width: 50rpx;
    // height: 140rpx;
    // width: 140rpx;
  }
	.img-text {
		white-space: nowrap;
    color: #fff;
    font-size: 30rpx;
    line-height: 30rpx;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    padding-bottom: 16rpx;
  }
</style>